package lxt.action;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import lxt.action.core.ActionSupport;
import lxt.action.core.DateUtil;
import lxt.entity.ChargeRecord;
import lxt.entity.Parameter;
import lxt.entity.UserInfo;
import lxt.service.ChargeRecordService;
import lxt.service.ClientService;
import lxt.service.InvoiceOrderService;

public class ChargeRecordAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	/**
	 * 定义成员变量,用于查询条件
	 */
	private String id;
    private String clientName;
    private String clientId;
    private String orderId;
    private String oweFee;
    private String realPayFee;
    private String chargeType;
    private String chargeDate;
    private String nextChargeDate;
    private String startDate;
    private String endDate;
    private String note;
    private String nextChargeFeeStart;
    private String nextChargeFeeEnd;
    private String deleteIds;
    /**
     * 定义接口
     */
    private ChargeRecordService chargeRecordService;
    private ClientService clientService;
    private InvoiceOrderService invoiceOrderService;
    /**
	 * 返回结果集
	 */
	private List<Map<String, Object>> chargeRecords;
	ChargeRecord chargeRecord;
	/**
	 * 定义对象
	 */
	 private List<Parameter> paramter ;
	/**
	 * 定义分页成员变量
	 */
	private Integer pageSize = 10;
	private String currPage = "1";
	private Integer total;
	
	private List<?> invoiceOrders;
	private String nextChargeFee;
    
	/**
	 * 通过构造方法实现查询时间段的初始化
	 */
	public ChargeRecordAction(){
		super();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar ca = Calendar.getInstance();
		this.endDate = sdf.format(ca.getTime());
		ca.add(Calendar.DAY_OF_YEAR, -30);
		this.startDate = sdf.format(ca.getTime());
	}
	
	@SuppressWarnings("finally")
	public String loadOrder(){
		try{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date start = null, end = null;
			if(startDate != null && !"".equals(startDate)){
				start = sdf.parse(startDate);
			}
			if(endDate != null && !"".equals(endDate)){
				end = sdf.parse(endDate);
			}
			//定义从第几条显示
			int offset = (Integer.parseInt(currPage) - 1) * pageSize;
			invoiceOrders = chargeRecordService.loadByPageWithClient(clientId, start, end, offset, pageSize);
			total = chargeRecordService.loadByPageWithClient(clientId, start, end, null, null).size();
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage(ex.getMessage());
		}finally{
			return SUCCESS;
		}
	}
	
	/**
	 * 分页查询
	 * @return
	 */
	
	@SuppressWarnings("finally")
	public String loadAll(){
		try{
			//定义从第几条显示
			int offset = (Integer.parseInt(currPage) - 1) * pageSize;
			chargeRecords = chargeRecordService.loadCharge(orderId, clientName, startDate, endDate, offset, pageSize);
			total = chargeRecordService.loadCharge(orderId, clientName, startDate, endDate, null, null).size();
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage(ex.getMessage());
		}
		finally{
			return SUCCESS;
		}
	}
	/**
	 * 添加运费预处理
	 * @return
	 */
	public String addPre(){
		return SUCCESS;
	}
	/**
	 * 添加运费
	 * @return
	 */
	@SuppressWarnings("finally")
	public String add(){
		try{
			ChargeRecord chargeRecord = new ChargeRecord();
			chargeRecord.setClientInfo(clientService.loadClientById(clientId));
			chargeRecord.setInvoiceOrder(invoiceOrderService.loadInvoiceOrderById(orderId));
			chargeRecord.setOweFee(Double.parseDouble(oweFee));
			chargeRecord.setRealPayFee(Double.parseDouble(realPayFee));
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			chargeRecord.setChargeDate(sdf.parse(chargeDate));
			chargeRecord.setNextChargeFee(Double.parseDouble(nextChargeFee));
			if(nextChargeDate!=null&&!nextChargeDate.equals("")){
				chargeRecord.setNextChargeDate(sdf.parse(nextChargeDate));
			}
			//获取操作员
			UserInfo user = (UserInfo) this.getSession().get("user");
			chargeRecord.setUsername(user.getUsername());
			chargeRecord.setNote(note);
			this.chargeRecordService.addCharge(chargeRecord);
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage(ex.getMessage());
		}
		finally{
			return SUCCESS;
		}
	}
	
	/**
	 * 准备更新数据
	 * @return
	 */
	public String updatePre(){
		try{
			chargeRecord = chargeRecordService.loadChargeById(id);
			/**
			 * 查看用户操作时间 设置编辑权限
			 */
			UserInfo user = (UserInfo) this.getSession().get("user");
			if(user.getLoginType()!=0){//记录员
				Date preTime = chargeRecord.getEntryTime();
				int falg = DateUtil.getDays(preTime);
				if(falg!=0){
					this.addActionMessage("无权修改数据,请联系管理员");
					this.loadAll();
					return INPUT;
				}
			}
			invoiceOrders = chargeRecordService.queryResumeForUpdatePre(chargeRecord);
			System.out.println(chargeRecord == null);
			//检查是否为最后一次收费记录
			int count = chargeRecordService.countLast(chargeRecord);
			if(count > 0) throw new Exception("当前收费记录不是最后一条,不能编辑.若要编辑,请先删除在该记录之后的缴费记录.");
		}catch(Exception e){
			e.printStackTrace();
			this.addActionMessage(e.getMessage());
			this.loadAll();
			return INPUT;
		}
		return SUCCESS;
	}
	
	public String update(){
		try{
			chargeRecord = chargeRecordService.loadChargeById(id);
			
			chargeRecord.setRealPayFee(Double.parseDouble(realPayFee));
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			chargeRecord.setChargeDate(sdf.parse(chargeDate));
			chargeRecord.setNextChargeFee(Double.parseDouble(nextChargeFee));
			if(nextChargeDate!=null&&!nextChargeDate.equals("")){
				chargeRecord.setNextChargeDate(sdf.parse(nextChargeDate));
			}
			//获取操作员
			UserInfo user = (UserInfo) this.getSession().get("user");
			chargeRecord.setUsername(user.getUsername());
			chargeRecord.setNote(note);
			this.chargeRecordService.updateCharge(chargeRecord);
		}catch(Exception e){
			e.printStackTrace();
			this.addActionMessage(e.getMessage());
			return INPUT;
		}
		return SUCCESS;
	}
	
	/**
	 * 删除收费信息
	 * @return
	 */
	@SuppressWarnings("finally")
	public String delete(){
		try{
			/**
			 * 查看用户操作时间 设置编辑权限
			 */
			UserInfo user = (UserInfo) this.getSession().get("user");
			if(user.getLoginType()!=0){//记录员
				for(int i=0;i<deleteIds.length();i++){
					String[] delId = deleteIds.split(",");
					ChargeRecord c = this.chargeRecordService.loadChargeById(delId[i]);
					int falg = DateUtil.getDays(c.getEntryTime());
					if(falg!=0){
						this.addActionMessage("无权修改数据,请联系管理员");
						this.loadAll();
						return INPUT;
					}
				}
			}
			chargeRecordService.deleteCharge(deleteIds);
		}catch(Exception e){
			e.printStackTrace();
			this.addActionMessage(e.getMessage());
		}finally{
			return SUCCESS;
		}
	}
	
	/**
	 * get set
	 * @return
	 */
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getClientName() {
		return clientName;
	}
	public void setClientName(String clientName) {
		this.clientName = clientName;
	}
	public String getOrderId() {
		return orderId;
	}
	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}
	public String getOweFee() {
		return oweFee;
	}
	public void setOweFee(String oweFee) {
		this.oweFee = oweFee;
	}
	public String getRealPayFee() {
		return realPayFee;
	}
	public void setRealPayFee(String realPayFee) {
		this.realPayFee = realPayFee;
	}
	public String getChargeType() {
		return chargeType;
	}
	public void setChargeType(String chargeType) {
		this.chargeType = chargeType;
	}
	public String getChargeDate() {
		return chargeDate;
	}
	public void setChargeDate(String chargeDate) {
		this.chargeDate = chargeDate;
	}
	public String getNextChargeDate() {
		return nextChargeDate;
	}
	public void setNextChargeDate(String nextChargeDate) {
		this.nextChargeDate = nextChargeDate;
	}

	public String getStartDate() {
		return startDate;
	}
	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}
	public String getEndDate() {
		return endDate;
	}
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	public String getNote() {
		return note;
	}
	public void setNote(String note) {
		this.note = note;
	}
	public String getNextChargeFeeStart() {
		return nextChargeFeeStart;
	}
	public void setNextChargeFeeStart(String nextChargeFeeStart) {
		this.nextChargeFeeStart = nextChargeFeeStart;
	}
	public String getNextChargeFeeEnd() {
		return nextChargeFeeEnd;
	}
	public void setNextChargeFeeEnd(String nextChargeFeeEnd) {
		this.nextChargeFeeEnd = nextChargeFeeEnd;
	}
	public ChargeRecordService getChargeRecordService() {
		return chargeRecordService;
	}
	public void setChargeRecordService(ChargeRecordService chargeRecordService) {
		this.chargeRecordService = chargeRecordService;
	}
	public List<Map<String, Object>> getChargeRecords() {
		return chargeRecords;
	}
	public Integer getPageSize() {
		return pageSize;
	}
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	public String getCurrPage() {
		return currPage;
	}
	public void setCurrPage(String currPage) {
		this.currPage = currPage;
	}
	public Integer getTotal() {
		return total;
	}
	public void setTotal(Integer total) {
		this.total = total;
	}
	public List<Parameter> getParamter() {
		return paramter;
	}
	public void setParamter(List<Parameter> paramter) {
		this.paramter = paramter;
	}

	public List<?> getInvoiceOrders() {
		return invoiceOrders;
	}

	public String getClientId() {
		return clientId;
	}

	public void setClientId(String clientId) {
		this.clientId = clientId;
	}

	public String getNextChargeFee() {
		return nextChargeFee;
	}

	public void setNextChargeFee(String nextChargeFee) {
		this.nextChargeFee = nextChargeFee;
	}

	public void setClientService(ClientService clientService) {
		this.clientService = clientService;
	}

	public void setInvoiceOrderService(InvoiceOrderService invoiceOrderService) {
		this.invoiceOrderService = invoiceOrderService;
	}

	public void setDeleteIds(String deleteIds) {
		this.deleteIds = deleteIds;
	}

	public ChargeRecord getChargeRecord() {
		return chargeRecord;
	}
}
